#include <sys/types.h>
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <errno.h>
#include <string.h>

#include "common.h"


int client_connect_tracker(char *serverip)
{
    struct sockaddr_in server;
    int sockfd;

    if ((sockfd = socket(AF_INET,SOCK_STREAM,0)) < 0) {
        printf("create socket failed\n");
        return -1;
    }
    server.sin_family = AF_INET;
    server.sin_port = htons(CLIPORT);
    inet_aton(serverip,&server.sin_addr);

    if (connect(sockfd,(struct sockaddr*)&server,sizeof(server)) < 0) {
        printf("connect failed: %s",strerror(errno));
        close(sockfd);
        return -1;
    }
    return sockfd;
}

void usage()
{
    fprintf(stderr,"client serverip\n");
}

void test1(int sockfd)
{
    char buf[128]= {0};
    struct cadoop_header *head;
    char *p = buf;
    int len = 0;
    
    head = (struct cadoop_header*)buf;
    head->magic = htonl(MAGIC_NUMBER);
    head->type = 0x1;
    head->cmd = 0x1;
    head->checksum = htons(0x1234);
    head->payload = htonl(4);

    p += sizeof(struct cadoop_header);
    strncpy(p,"work",4);
    len = write(sockfd,buf,sizeof(struct cadoop_header)+4);
    int i;
    for (i=0;i<len;i++) {
        printf("%02x ",buf[i]);
    }
    printf("write %d\n",len);
    return;
}

void test2(int sockfd)
{
    char buf[128]= {0};
    struct cadoop_header *head;
    char *p = buf;
    int len = 0;
    
    head = (struct cadoop_header*)buf;
    head->magic = htonl(MAGIC_NUMBER);
    head->type = 0x1;
    head->cmd = 0x1;
    head->checksum = htons(0x1234);
    head->payload = htonl(4);

    p += sizeof(struct cadoop_header);
    strncpy(p,"work",4);
    int i;
    printf("write on byte every mimute\n");
    p = buf;
    for(i=0;i<16;i +=4) {
        printf("%02x ",buf[i]);
        len = write(sockfd,p,4);
        p+=4;
        sleep(1);
    }

    printf("test2 end\n");
#if 0
    len = write(sockfd,buf,sizeof(struct cadoop_header)+4);
    int i;
    for (i=0;i<len;i++) {
        printf("%02x ",buf[i]);
    }
    printf("write %d\n",len);
#endif
    return;
}

void test3(int sockfd)
{
    char buf[128] = {0};

    struct cadoop_header *head = (struct cadoop_header*)buf;
    struct dfs_metadata *meta = (struct dfs_metadata*)(buf + sizeof(struct cadoop_header));
    head->magic = htonl(MAGIC_NUMBER);
    head->type = 0x01;
    head->cmd = CH_CLIENT_CREATE_FILE;
    head->checksum = 0x00;
    head->payload = htonl(16);

    meta->version = htonl(0x09);
    meta->filesize = htonl(0xFF);
    meta->fsnamelen = htonl(4);
    strcpy(meta->filename,"hell");
    write(sockfd,buf,28);
    write(sockfd,buf,28);
    return;
}

int main(int argc,char **argv)
{
    int sockfd;

    if (argc !=2) {
        usage();
        return -1;
    }
    sockfd = client_connect_tracker(argv[1]);
    //test1(sockfd);
    printf("##########\n");
    //test2(sockfd); 
    //printf("**********\n"); 
    test3(sockfd);
    getchar();
    close(sockfd);

    printf("#####client#####\n");
    return 0;
}
